package com.chennan.excelpoi;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.CellExtra;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.chennan.excelpoi.model.CardMoadl;
import com.chennan.excelpoi.model.CardProperty;
import com.chennan.excelpoi.service.CardImportService;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author chennan
 * @create 2022-09
 */
@Slf4j
public class NoModelCardImport extends AnalysisEventListener<Map<Integer, String>> {

    /**
     * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 100;
    private List<Object> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    private CardImportService cardImportService;

    private List<CardProperty> cardProperties = new ArrayList<>();

    public NoModelCardImport(CardImportService cardImportService){
        this.cardImportService = cardImportService;
    }

    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {

    }

    @Override
    public void invoke(Map<Integer, String> integerStringMap, AnalysisContext analysisContext) {
//        log.info("解析到一条数据:{}", JSON.toJSONString(integerStringMap));
        Integer rowIndex = analysisContext.readRowHolder().getRowIndex();
        String sheetName = analysisContext.readSheetHolder().getSheetName();
        int size = integerStringMap.size();
        if(rowIndex.equals(1)){
            //属性名行
            for (int i = 0; i < size; i++) {
                String value = integerStringMap.get(i);
                CardProperty cardProperty = new CardProperty();
                cardProperty.setPropertyName(value);
                cardProperties.add(cardProperty);
            }
        }else if(rowIndex.equals(2)){
            //属性标题行
            for (int i = 0; i < size; i++) {
                String value = integerStringMap.get(i);
                CardProperty cardProperty = cardProperties.get(i);
                cardProperty.setPropertyTitle(value);
            }
        }else if(rowIndex>3){
            List<CardProperty> properties = new ArrayList<>(cardProperties);
            for (int i = 0; i < size; i++) {
                String value = integerStringMap.get(i);
                properties.get(i).setValue(value);
            }
            CardMoadl cardMoadl = new CardMoadl();
            cardMoadl.setData(properties);
            cachedDataList.add(cardMoadl);
        }
        if (cachedDataList.size() >= BATCH_COUNT) {
            saveData();
            cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }
    @Override
    public void extra(CellExtra extra, AnalysisContext context) {

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
        log.info("所有数据解析完成！");
        cardProperties = new ArrayList<>();
    }

//    @Override
//    public boolean hasNext(AnalysisContext context) {
//        return false;
//    }

    /**
     * 加上存储数据库
     */
    private void saveData() {
        cardImportService.saveMap(cachedDataList);
        log.info("存储数据库成功！");
    }

}
